﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Comm
{
    /// <summary>
    ///AdminPageBase 的摘要说明
    /// </summary>
    public class AdminPageBase : PageBase
    {
        private string sessionname = "admin";
        public AdminPageBase()
        {

        }

        /// <summary>
        /// 获取Url
        /// </summary>
        /// <returns></returns>
        protected string GetUrl()
        {
            return Server.UrlEncode(System.IO.Path.GetFileName(Request.PhysicalPath) + Request.Url.Query);
        }

        /// <summary>
        /// 获取返回的Url
        /// </summary>
        /// <returns></returns>
        protected string GetRedirectUrl()
        {
            string Param = Request.Url.Query;
            string url = System.Text.RegularExpressions.Regex.Replace(Param, ".*from=", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            return Server.UrlDecode(url);
        }

        protected override void OnPreInit(EventArgs e)
        {
            CheckSession();
        }

        /// <summary>
        /// 验证Session
        /// </summary>
        private void CheckSession()
        {
            if (Session[sessionname] == null)
            {
                Response.Redirect("/web_admin/login.aspx");
            }
        }

        /// <summary>
        /// 检查是否有改权限
        /// </summary>
        protected void CheckPopedomInfoRedirect(int ModuleID,long Role)
        {
            BLL.TPermission bll = new BLL.TPermission();
            List<Model.TPermission> list = bll.DataTableToList(bll.GetList(1,"RoleID="+myuser.RoleID.Value+" and FormID="+ModuleID,"FormID asc").Tables[0]);
            if (list == null || list.Count < 1 || (long.Parse(list[0].Permission.ToString()) & Role)!=Role)
            {
                MessageBox.Show("您没有查看该模块的权限");
            }
        }

        /// <summary>
        /// 检查是否有改权限
        /// </summary>
        protected bool CheckPopedomInfo(int ModuleID, long Role)
        {
            BLL.TPermission bll = new BLL.TPermission();
            List<Model.TPermission> list = bll.DataTableToList(bll.GetList(1, "RoleID=" + myuser.RoleID.Value + " and FormID=" + ModuleID, "FormID asc").Tables[0]);
            if (list == null || list.Count < 1 || (long.Parse(list[0].Permission.ToString()) & Role) != Role)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获取管理员
        /// </summary>
        protected Model.Admin myuser
        {
            get
            {
                Model.Admin model = Session[sessionname] as Model.Admin;
                if (model == null)
                {
                    Response.Redirect("/web_admin/login.aspx");
                }
                return model;
            }
        }


        /// <summary>
        /// 添加管理员日志
        /// </summary>
        /// <param name="TypeID">操作类型 0：登陆 1：添加 2：修改 3：删除</param>
        /// <param name="Context">操作描述(格式：类型 操作描述)</param>
        /// <returns>返回结果</returns>
        protected bool AddLog(int TypeID, string Context)
        {
            Model.AdminLog model = new Model.AdminLog();
            model.UserName = myuser.Username;
            model.OperationTypes = TypeID;
            model.ODescription = Context;
            model.IP = Request.UserHostAddress.Trim();
            model.AddTime = DateTime.Now;

            return new BLL.AdminLog().Add(model) > 0 ? true : false;
        }

        /// <summary>
        /// 获取状态
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected string getStateImage(object obj)
        {
            string image = "no";
            if (obj != null && Convert.ToBoolean(obj))
            {
                image = "ok";
            }

            return string.Format("<img src=\"../images/default/{0}Ico.gif\" width=\"11\" height=\"11\" />", image);
        }
    }

}